#include "Unit.h"
#include "Level.h"
#include "ImgMgr.h"


Unit::Unit()
{
}

bool Unit::getDead() const
{
	return m_dead;
}

sf::Vector2f Unit::getPosition() const
{
	return m_skin[m_currentRender].getPosition();
}

void Unit::setDirection(const float angle)
{
	m_moveX = (float)std::cos(angle * PI / 180);
	m_moveY = (float)std::sin(angle * PI / 180);
	if(m_moveX < 0.5 && m_moveX > -0.5 && m_moveY > 0)
		m_direction = 1;
	if(m_moveX < 0.5 && m_moveX > -0.5 && m_moveY < 0)
		m_direction = 2;
	if(m_moveY < 0.5 && m_moveY > -0.5 && m_moveX > 0)
		m_direction = 3;
	if(m_moveY < 0.5 && m_moveY > -0.5 && m_moveX < 0)
		m_direction = 4;
}

int Unit::random(int a, int b)
{
    return rand()%(b-a) +a;
}

void Unit::setDommage(const float degat)
{
	m_health -= degat;
}

void Unit::init(const int idTeam, const sf::Vector2f position, ImgMgr *imageManager)
{
	m_dead = false;
	m_idTeam = idTeam;
	m_direction = 0;
	m_moveX = 0;
	m_moveY = 0;
	m_changeDirection = random(500, 2000);
	m_originHealth = m_health;
	m_barHealth.setSize(sf::Vector2f(m_skin[m_currentRender].getGlobalBounds().width / 2, 2));
	m_barHealth.setFillColor(sf::Color::Red);
	int i = 0;
	while(i < 12)
	{
		m_skin[i].setPosition(position);
		i++;
	}
	m_shadow.setSize(sf::Vector2f(m_skin[m_currentRender].getGlobalBounds().width / 2, 1));
	m_shadow.setFillColor(sf::Color::Color(0, 0, 0, 125));
}

void Unit::maj(Level *level)
{
	bool collisionUp = false;
	bool collisionDown = false;
	bool collisionLeft = false;
	bool collisionRight = false;

	if(level->collisionTile(getPosition(), 1))
		collisionDown = true;	
	if(level->collisionTile(getPosition(), 1))
		collisionDown = true;
	if(level->collisionTile(getPosition(), 3))
		collisionRight = true;
	if(level->collisionTile(getPosition(), 4))
		collisionLeft = true;

	if(m_clockFrame.getElapsedTime().asMilliseconds() > 150)
	{
		if(m_direction == 1)
		{
			m_currentRender++;
			if(m_currentRender > 1)
				m_currentRender = 0;
		}
		else if(m_direction == 2)
		{
			m_currentRender++;
			if(m_currentRender < 2 || m_currentRender > 3)
				m_currentRender = 2;
		}
		else if(m_direction == 3)
		{
			m_currentRender++;
			if(m_currentRender < 4 || m_currentRender > 5)
				m_currentRender = 4;
		}
		else if(m_direction == 4)
		{
			m_currentRender++;
			if(m_currentRender < 6 || m_currentRender > 7)
				m_currentRender = 6;
		}
		m_clockFrame.restart();
	}
	if(m_moveY < 0)
	{
		if(!collisionUp)
			m_skin[m_currentRender].move(0, m_moveY * m_speed);
	}
	else if(m_moveY > 0)
	{
		if(!collisionDown)
			m_skin[m_currentRender].move(0, m_moveY * m_speed);
	}
	if(m_moveX < 0)
	{
		if(!collisionLeft)
			m_skin[m_currentRender].move(m_moveX * m_speed, 0);
	}
	else if(m_moveX > 0)
	{
		if(!collisionRight)
			m_skin[m_currentRender].move(m_moveX * m_speed, 0);
	}
	int e = 0;
	while(e < 12)
	{
		if(e != m_currentRender)
			m_skin[e].setPosition(m_skin[m_currentRender].getPosition());
		e++;
	}
	m_shadow.setPosition(sf::Vector2f(getPosition().x + m_shadow.getGlobalBounds().width / 2, getPosition().y + m_skin[m_currentRender].getGlobalBounds().height - 1));
	if(m_health < m_originHealth)
	{
		float pourcentageRest = m_health / m_originHealth * 100;
		m_barHealth.setSize(sf::Vector2f(m_skin[m_currentRender].getGlobalBounds().width / 2 / 100 * pourcentageRest , 2));
		m_barHealth.setPosition(sf::Vector2f(getPosition().x + 8 - m_barHealth.getGlobalBounds().width / 2, getPosition().y));
	}
	if(m_health <= 0)
		m_dead = true;
}

void Unit::render(sf::RenderWindow *app)
{
	if(!m_dead)
	{
		app->draw(m_skin[m_currentRender]);
		app->draw(m_shadow);
		if(m_health < m_originHealth)
			app->draw(m_barHealth);
	}
}